//
//  visitorView.swift
//  我的微博
//
//  Created by shmily on 15/7/29.
//  Copyright © 2015年 Stanford-IOS8-Swift-shmilyAshen. All rights reserved.
//

import UIKit

///注册登陆协议
protocol VisitorViewDelegate :NSObjectProtocol{
    ///注册按钮事件
    func visitorViewRegistor()
    ///登陆按钮事件
    func visitorViewLogin()
}

class VisitorView: UIView {

    ///代理
    var delegate : VisitorViewDelegate?
    
   override init(frame: CGRect) {
        super.init(frame: frame)
    
        setUpView()
    }

   required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUpView()
   }
    
    //MARK: - 设置视图
   private func setUpView() {
        // 设置视图背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
    
        addSubview(imgRound)
        addSubview(imgMask)
        addSubview(imgMain)
        addSubview(lblTips)
        addSubview(btnRegister)
        addSubview(btnLogin)
    
        setAutoLayout()
    }
    
    ///设置显示信息
    func setUpInfo(isHome:Bool,imgIcon : String,title : String) {
        imgMain.image = UIImage(named: imgIcon)
        lblTips.text = title
        
        imgRound.hidden = !isHome
        
        isHome ? startAnimation() : sendSubviewToBack(imgMask)
    }
    
    ///执行动画
    private func startAnimation() {
       let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.repeatCount = MAXFLOAT
        ani.duration = 20.0
        ani.removedOnCompletion = false
        
        imgRound.layer.addAnimation(ani, forKey: nil)
    }
    
    //MARK: - 代理注册登陆方法
    func registorBtn() {
        delegate?.visitorViewRegistor()
    }
    
    func loginBtn() {
        delegate?.visitorViewLogin()
    }
    
    
    //MARK: - 设置自动布局
    
    ///设置自动布局
    private func setAutoLayout() {
        //主要图片的自动布局
        imgMain.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: imgMain, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
        addConstraint(NSLayoutConstraint(item: imgMain, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        //遮罩mask布局
        imgMask.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: imgMask, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: imgMask, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 5))
        
        //旋转图片布局
        imgRound.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: imgRound, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: imgRound, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //文本框布局
        lblTips.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: lblTips, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: lblTips, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 224))
        addConstraint(NSLayoutConstraint(item: lblTips, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imgMain, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        //注册按钮
        btnRegister.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: btnRegister, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: lblTips, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: btnRegister, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lblTips, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: btnRegister, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        addConstraint(NSLayoutConstraint(item: btnRegister, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
        
        //登陆按钮
         btnLogin.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: lblTips, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lblTips, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 100))
        addConstraint(NSLayoutConstraint(item: btnLogin, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 35))
    }
    
    
    // MARK: - 视图懒加载
    
    //显示旋转图像
    lazy var imgRound : UIImageView = {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return imgView
        }()
    //显示遮罩 mask
    lazy var imgMask:UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imgView
    }()
    
    //显示主要图片
    lazy var imgMain : UIImageView = {
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "visitordiscover_feed_image_house")
        return imgView
    }()
    
    //显示文本框
    lazy var lblTips : UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFontOfSize(14)
        lbl.numberOfLines = 0
        lbl.textColor = UIColor.darkGrayColor()
        lbl.textAlignment = NSTextAlignment.Center
        return lbl
    }()
    
    //注册按钮
    lazy var btnRegister:UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registorBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //登陆按钮
    lazy var btnLogin:UIButton = {
        let btn = UIButton()
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
         btn.addTarget(self, action: "loginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}
